package com.cloudcc.mobile.view.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.android.pushservice.PushManager;
import com.cloudcc.cloudframe.util.SharePreferece;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.im.IMHelper;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.service.DingWeiService;
import com.cloudcc.mobile.util.DialogUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.login.LoginNewActivity;
import com.cloudcc.mobile.weight.SlideSwitch;
import com.cloudcc.mobile.weight.SlideSwitch.OnChangeListener;
import com.easemob.EMCallBack;

@Deprecated
public class SettingActivity extends BaseActivity{
	private SharePreferece share;
	private LinearLayout exit,guanyu,zhuxiao,shengyin,zhendong,time,huancun,weizhi,xiaoxi;
	private TextView timeset;
	private SlideSwitch locationSet,mesSet,zhengdongSet,shengyinSet;
	private Context context;
	private ImageView menubtn;
	private Builder builder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		try {
			context=this;
			setupView();
			share();
			addListener();
		} catch (Exception e) {
			Tools.handle(e);
		}
	}
	@Override
	public int getLayoutId() {
		return R.layout.setting2;
	}
	private void share() {
		Map<String,?> locationmap=share.getShare(UserManager.getManager().getUser().userId+"islocation");
		Map<String,?> pushmap=share.getShare(UserManager.getManager().getUser().userId+"ispush");
		if(locationmap==null||locationmap.isEmpty()){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("time", "1200000");
			map.put("islocation", true);
			share.SaveShare(UserManager.getManager().getUser().userId+"islocation", map);
		}else{
			boolean islocation=(Boolean) locationmap.get("islocation");
			String time=(String) locationmap.get("time");
			if(time.equals("300000")){
				timeset.setText("5分钟");
			}else if(time.equals("600000")){
				timeset.setText("10分钟");
			}else if(time.equals("1200000")){
				timeset.setText("20分钟");
			}else if(time.equals("1800000")){
				timeset.setText("30分钟");
			}else if(time.equals("3600000")){
				timeset.setText("1小时");
			}else if(time.equals("7200000")){
				timeset.setText("2小时");
			}
			locationSet.setSelected(islocation);

		}
		if(!(pushmap==null||pushmap.isEmpty())){
			boolean ispush=(Boolean) pushmap.get("ispush");
			boolean isShengyin=(Boolean) pushmap.get("isShengyin");
			boolean isZhengdong=(Boolean) pushmap.get("isZhengdong");
			mesSet.setSelected(ispush);
			shengyinSet.setSelected(isShengyin);
			zhengdongSet.setSelected(isZhengdong);
		}else{
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("ispush", true);
			map.put("isShengyin", true);
			map.put("isZhengdong", true);
			share.SaveShare(UserManager.getManager().getUser().userId+"ispush", map);
		}

	}
	/*
	 * 获取控件
	 */
	private void addListener() {
		guanyu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context,CloudccActivity.class);
				startActivity(intent);
			}
		});
		/**
		 * 打开菜单
		 */
		menubtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SettingActivity.this.finish();
			}
		});
		/**
		 * 注销登录
		 */
		zhuxiao.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showClearLogin(context);
			}
		});
		//时间设置
		time.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			showDialog();
		}});
		//清除缓存
		huancun.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			DialogUtils.showClearCache(context);
		}});
		//退出
		exit.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			DialogUtils.showExit(context);
		}});
		//位置上报
		weizhi.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			if(locationSet.isSelected()){
				locationSet.setSelected(false);
			}else{
				locationSet.setSelected(true);
			}
		}});
		//消息监听
		xiaoxi.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			if(mesSet.isSelected()){
				mesSet.setSelected(false);
			}else{
				mesSet.setSelected(true);
			}
		}

		});
		//消息监听
		shengyin.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			if(shengyinSet.isSelected()){
				shengyinSet.setSelected(false);
			}else{
				shengyinSet.setSelected(true);
			}
		}});
		//震动
		zhendong.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {
			if(zhengdongSet.isSelected()){
				zhengdongSet.setSelected(false);
			}else{
				zhengdongSet.setSelected(true);
			}
		}});
		//消息监听
		mesSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setMessageListener(state);
			}
		});
		//声音监听
		shengyinSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setShengyinListener(state);
			}
		});
		//震动监听
		zhengdongSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setZhendongListener(state);
			}
		});

		//位置上报
		locationSet.setOnChangeListener(new OnChangeListener() {
			@Override
			public void onChange(SlideSwitch sb, boolean state) {
				setLocationListener(state);
			}
		});
	}

	//位置上报监听
	public void setLocationListener(boolean state) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("islocation", state);
		String time=timeset.getText().toString().trim();
		String times = "300000";
		if(time.equals("5分钟")){
			times="300000";
		}else if(time.equals("10分钟")){
			times="600000";
		}else if(time.equals("15分钟")){
			times="900000";
		}else if(time.equals("30分钟")){
			times="1800000";
		}else if(time.equals("1小时")){
			times="3600000";
		}
		map.put("time", times);
		share.SaveShare(UserManager.getManager().getUser().userId+"islocation", map);
		Intent intent=new Intent(context,DingWeiService.class);
		if(state){
			AppContext.isStopLocation=false;
			context.startService(intent);
		}else{
			AppContext.isStopLocation=true;
			context.stopService(intent);
		}
	}
	//设置震动监听
	public void setZhendongListener(boolean istrue) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("isZhengdong", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId+"ispush", map);
	}
	//设置声音监听
	public void setShengyinListener(boolean istrue) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("isShengyin", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId+"ispush", map);
	}
	//设置消息提醒监听
	public void setMessageListener(boolean istrue) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ispush", istrue);
		share.SaveShare(UserManager.getManager().getUser().userId+"ispush", map);
		if(istrue){
			PushManager.resumeWork(context);
		}else{
			PushManager.stopWork(context);
		}
	}
	/**
	 * 设置dialog
	 */
	protected void showDialog() {
		AlertDialog.Builder builder=new AlertDialog.Builder(context);
		builder.setTitle("上报时间设置");
		final String[] items=new String[]{"5分钟","10分钟","20分钟","30分钟","1小时","2小时"};
		String time=timeset.getText().toString().trim();
		int item = 0;
		if(time.equals("5分钟")){
			item=0;
		}else if(time.equals("10分钟")){
			item=1;
		}else if(time.equals("20分钟")){
			item=2;
		}else if(time.equals("30分钟")){
			item=3;
		}else if(time.equals("1小时")){
			item=4;
		}else if(time.equals("2小时")){
			item=5;
		}
		builder.setSingleChoiceItems(items, item, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				AppContext.time=items[which];
			}
		});
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				timeset.setText(AppContext.time);
				Map<String, Object> map=new HashMap<String, Object>();
				String time=timeset.getText().toString().trim();
				String times = "300000";
				if(time.equals("5分钟")){
					times="300000";
					AppContext.locationtime=300000;
				}else if(time.equals("10分钟")){
					times="600000";
					AppContext.locationtime=600000;
				}else if(time.equals("20分钟")){
					times="1200000";
					AppContext.locationtime=1200000;
				}else if(time.equals("30分钟")){
					times="1800000";
					AppContext.locationtime=1800000;
				}else if(time.equals("1小时")){
					times="3600000";
					AppContext.locationtime=3600000;
				}else if(time.equals("2小时")){
					times="7200000";
					AppContext.locationtime=7200000;
				}
				map.put("time", times);
				share.SaveShare(UserManager.getManager().getUser().userId+"islocation", map);

			}});
		builder.setNegativeButton("取消", null);
		builder.show();
	}
	//获取控件
	private void setupView() {
		mesSet=(SlideSwitch)findViewById(R.id.setmessage);
		locationSet=(SlideSwitch)findViewById(R.id.setloaction);
		shengyinSet=(SlideSwitch)findViewById(R.id.setshengyin);
		zhengdongSet=(SlideSwitch)findViewById(R.id.setzhengdong);
		xiaoxi=(LinearLayout)findViewById(R.id.xiaoxi);
		weizhi=(LinearLayout)findViewById(R.id.weizhi);
		exit=(LinearLayout)findViewById(R.id.exit);
		huancun=(LinearLayout)findViewById(R.id.huancun);
		zhuxiao=(LinearLayout)findViewById(R.id.zhuxiao);
		guanyu=(LinearLayout)findViewById(R.id.guanyu);
		zhendong=(LinearLayout)findViewById(R.id.zhendong);
		shengyin=(LinearLayout)findViewById(R.id.shengyin);
		time=(LinearLayout)findViewById(R.id.time);
		timeset=(TextView)findViewById(R.id.timeset);
		menubtn=(ImageView)findViewById(R.id.menu_setting);
		share=new SharePreferece(context);
		builder=new AlertDialog.Builder(context);
	}
	/**
	 * 注销登录
	 * @param context
	 */
	public  void showClearLogin(final Context context){
		AlertDialog.Builder   builder=new AlertDialog.Builder(context);
		builder.setMessage("亲！确认注销吗？");
		builder.setTitle("注销账号");
		builder.setNegativeButton("确认", new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//退出环信
				IMHelper.getInstance().logout(false, new EMCallBack() {
					
					@Override
					public void onSuccess() {
						runOnUiThread(new Runnable() {
							public void run() {
								Intent intent=new Intent(context,LoginNewActivity.class);
								context.startActivity(intent);
								finish();
								
							}
						});
					}
					
					@Override
					public void onProgress(int arg0, String arg1) {
						
					}
					
					@Override
					public void onError(int arg0, String arg1) {
						Toast.makeText(mContext, "退出失败。。。", Toast.LENGTH_SHORT).show();
					}
				});

			}
		});
		builder.setPositiveButton("取消", null);
		builder.show();
	}
}
